package com.lgq.cfw.service.impl;

import com.lgq.cfw.bean.TablePermission;
import com.lgq.cfw.dao.TablePermissionMapper;
import com.lgq.cfw.service.PermissionService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Lin
 * @date 2019-02-10 15:41
 **/
@Service
public class PermissionServiceImpl implements PermissionService {

  @Autowired
  TablePermissionMapper tablePermissionMapper;

  /**
   * 获取所有权限
   * @return 所有权限的list
   */
  @Override
  public List<TablePermission> getAllPermission() {
    return tablePermissionMapper.selectByExample(null);
  }

  /**
   * 通过角色id获取权限
   * @param roleId 角色ID
   * @return 该角色的所有权限
   */
  @Override
  public List<TablePermission> getPermissionById(Integer roleId) {
    return tablePermissionMapper.getPermissionByRoleId(roleId);
  }

  /**
   * 删除某个权限
   * @param permissionId 权限id
   * @return 处理结果
   */
  @Override
  public Boolean deletePermission(Integer permissionId) {
    try {
      tablePermissionMapper.deleteByPrimaryKey(permissionId);
    }catch (Exception e){
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /**
   * 添加权限
   * @param pid 父菜单
   * @param name 权限名称
   */
  @Override
  public void addPermission(Integer pid,String name) {
    TablePermission tablePermission=new TablePermission();
    tablePermission.setName(name);
    tablePermission.setPid(pid);
    tablePermissionMapper.insert(tablePermission);
  }

  /**
   * 更新权限信息
   * @param tablePermission 前台疯转好的permission
   */
  @Override
  public void updatePermission(TablePermission tablePermission) {
    tablePermissionMapper.updateByPrimaryKeySelective(tablePermission);
  }
}
